package daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import connector.Connector;
import daointerfaces.DALException;
import daointerfaces.ReceptDAO;
import dto.ReceptDTO;

public class MySQLReceptDAO implements ReceptDAO{

	@Override
	public ReceptDTO getRecept(int receptId) throws DALException {
		ResultSet rs = Connector.doQuery("SELECT * FROM receptCDIO WHERE receptID = " + receptId);
		try {
			if (!rs.first()) throw new DALException("Recept " + receptId + " findes ikke");
			return new ReceptDTO(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getDouble(4), rs.getDouble(5));
		}
		catch (SQLException e) {throw new DALException(e); }
	}

	@Override
	public List<ReceptDTO> getReceptList() throws DALException {
		List<ReceptDTO> list = new ArrayList<ReceptDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM receptCDIO");
		try
		{
			while (rs.next())
			{
				list.add(new ReceptDTO(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getDouble(4), rs.getDouble(5)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	public List<ReceptDTO> getReceptListNr(int receptID) throws DALException {
		List<ReceptDTO> list = new ArrayList<ReceptDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM receptCDIO WHERE receptID = "+receptID);
		try
		{
			while (rs.next()) 
			{
				list.add(new ReceptDTO(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getDouble(4), rs.getDouble(5)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	@Override
	public List<ReceptDTO> getReceptListGroup() throws DALException {
		List<ReceptDTO> list = new ArrayList<ReceptDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM receptCDIO GROUP BY receptID");
		try
		{
			while (rs.next())
			{
				list.add(new ReceptDTO(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getDouble(4), rs.getDouble(5)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	@Override
	public void createRecept(ReceptDTO recept) throws DALException {
		Connector.doUpdate(
				"INSERT INTO receptCDIO(receptID, receptNavn, raavareID, nomNetto, tolerance) VALUES " +
						"(" + recept.getReceptId() + ", '" + recept.getReceptNavn() + "', " + recept.getRaavareId() + ", " + recept.getNomNetto() 
						+ ", " + recept.getTolerance() + ")"
				);
		System.out.println("SQL OK!");
	}

	public void deleteRecept(int receptId) throws DALException {
		Connector.doUpdate(
				"DELETE FROM receptCDIO WHERE receptId=" + receptId
				);
	}

	@Override
	public void updateRecept(ReceptDTO recept) throws DALException {
		Connector.doUpdate(
				"UPDATE receptCDIO SET  receptId = " + recept.getReceptId() + ", receptNavn = '" + recept.getReceptNavn() + "', raavareID = " 
						+ recept.getRaavareId() + ", nomNetto = " + recept.getNomNetto() + ", tolerance = " + recept.getTolerance()
						+ " WHERE receptID =  "  + recept.getReceptId()+" AND raavareID = "+recept.getRaavareId()
				);
	}
}
